package org.openfdtd.gui;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.vecmath.Vector3d;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openfdtd.core3d.Vertex;
import org.openfdtd.core3d.Surface;
import org.openfdtd.core3d.TransformUtil;
import org.openfdtd.core3d.SurfaceListenner;
import org.openfdtd.core3d.EditableObject3D;
import org.openfdtd.core3d.EditableUniverse;
import org.openfdtd.gui.components.ToolBar;
import org.openfdtd.gui.components.PopupMenu;

/**
 *
 * @author Dórian C. Langbeck
 */
public class MainFrame extends javax.swing.JFrame {
    private EditableObject3D objectAction;
    private EditableUniverse universe;
    private PopupMenu faceActions;
    private PopupMenu noneActions;

    public MainFrame(EditableUniverse universe) {
        initComponents();
        setLocationRelativeTo(null);
        setUniverse(universe);
        configureActions();
    }

    private JButton createMenuButton(Icon icone, String str) {
        JButton button = new JButton(icone);
        button.setFocusable(false);
        button.setToolTipText(str);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });
        return button;
    }

    private JButton createMenuButton(String iconeURL, String str) {
        return createMenuButton(new ImageIcon(getClass().getResource(iconeURL)), str);
    }

    private void configureActions() {
        faceActions = new PopupMenu();
        faceActions.addRotuleSeparator("Opções do objeto");
        faceActions.add("Excluir", new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                universe.remove(objectAction);
            }
        });
        faceActions.add("Arrastar", new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });

        noneActions = new PopupMenu();
        noneActions.add("Adicionar cubo", new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });

        universe.addSurfaceListenner(new SurfaceListenner() {
            public void mouseReleased(Surface face, EditableObject3D obj, MouseEvent e) {
                
            }

            public void mousePressed(Surface face, EditableObject3D obj, MouseEvent e) {

            }

            public void mouseEntered(Surface face, EditableObject3D obj, MouseEvent e) {

            }

            public void mouseClicked(Surface face, EditableObject3D obj, MouseEvent e) {
                if (obj == null)
                    return;

                switch (e.getButton()) {
                    case MouseEvent.BUTTON3:
                        faceActions.show(rootPane, e.getX(), e.getY());
                        universe.setActiveObject(obj);
                        objectAction = obj;
                        break;

                    case MouseEvent.BUTTON1:
                        TransformUtil.translate(face, new Vector3d(0.5, 0.5, 0.5));
                        break;
                }
            }

            public void mouseExited(Surface face, EditableObject3D obj, MouseEvent e) {

            }
        });
    }

    public void setUniverse(EditableUniverse universe) {
        this.universe = universe;
        
        canvasPanel.removeAll();
        canvasPanel.add(universe.getCanvas3D());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        canvasPanel = new javax.swing.JPanel();
        toolBar = new ToolBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OpenFDTD - Version: 9.5.08a");

        canvasPanel.setBackground(java.awt.Color.white);
        canvasPanel.setLayout(new java.awt.BorderLayout());

        toolBar.setFloatable(false);
        toolBar.setRollover(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addComponent(canvasPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(canvasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel canvasPanel;
    private javax.swing.JToolBar toolBar;
    // End of variables declaration//GEN-END:variables
}
